package com.aco.fatureapp.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;
import android.widget.TextView;
import br.com.ac.fatureweb.integration.utilitary.NumberUtils;

import com.aco.fatureapp.R;
import com.aco.fatureapp.auxiliary.ShowBillAdapter;
import com.aco.fatureapp.auxiliary.ShowBillItem;
import com.aco.fatureapp.model.Imovel;
import com.aco.fatureapp.model.ProductDetail;
import com.aco.fatureapp.model.billing.Produto.EnumTypeProduto;
import com.aco.fatureapp.service.SrvBilling;
import com.google.inject.Inject;

@SuppressLint("UseSparseArrays")
@ContentView(R.layout.activity_show_bill)
public class ShowBillActivity extends BaseActivity {

	@InjectView(R.id.txtVlrTotal)
	private TextView mTxtVlrTotal;

	@InjectView(R.id.lstItemConta)
	private ExpandableListView mLstItemConta;

	/*
	 * Service
	 */
	@Inject
	private SrvBilling mSrvBilling;

	/*
	 * Info
	 */
	private Long mInscricao;

	private ShowBillAdapter mAdapter;

	private List<ShowBillItem> mLstBillItem;

	private Map<Integer, List<ProductDetail>> mLstDetails;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.aco.fatureapp.activity.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mInscricao = getIntent().getExtras().getLong(
				Imovel.getTableName(Imovel.class));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see roboguice.activity.RoboActivity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();

		populateBillItens();
		populateDetails();

		mAdapter = new ShowBillAdapter(this, mLstBillItem,
				(HashMap<Integer, List<ProductDetail>>) mLstDetails);

		mLstItemConta.setAdapter(mAdapter);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		getActionBar().setDisplayHomeAsUpEnabled(true);

		return true;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		mSrvBilling.closeDB();
		super.onDestroy();
	}

	/**
	 * Preenchendo a lista principal com os produtos e lançamentos da inscrição.
	 */
	private void populateBillItens() {
		mLstBillItem = new ArrayList<ShowBillItem>();

		mLstBillItem.addAll(mSrvBilling.getLstProduto(mInscricao));
		mLstBillItem.addAll(mSrvBilling.getLstLancamentoCobrado(mInscricao));
	}

	/**
	 * Preenchendo as informações de detalhe com os dados de volume e valor.
	 */
	private void populateDetails() {
		mLstDetails = new HashMap<Integer, List<ProductDetail>>();

		Double valorTotalFatura = 0D;
		for (int i = 0; i < mLstBillItem.size(); i++) {
			ShowBillItem item = mLstBillItem.get(i);

			if (item.getCodigo() != null
					&& item.getCodigo().intValue() == EnumTypeProduto.AGUA
							.getCodigo()) {

				List<ProductDetail> productDetails = mSrvBilling
						.getLstProductDetail(mInscricao, item);

				mLstDetails.put(i, productDetails);
			}

			valorTotalFatura += item.getValor();
		}

		/*
		 * Valor total da fatura é o somatório entre o valor do produto e o
		 * valor de todos os lançamentos.
		 */
		mTxtVlrTotal.setText(NumberUtils.formatMoney(valorTotalFatura));
	}
}